﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReviewCurveApp.Classes;
using System.Data;

namespace ReviewCurveApp.Contorller
{
    public class TasksManager
    {
        DBLoader db = new DBLoader();
        internal bool DeleteRecord(Task task)
        {

            MyDBCommand command = new MyDBCommand();
            command.Sql = "Delete From test.Reviews Where UnitId=@UnitId and Sequence=@Sequence";
            command.Parameters.Add("@UnitId", task.UnitId);
            command.Parameters.Add("@Sequence", task.Sequnce);
            return db.ExcuteQuery(command);
        }

        internal int AddRecord(Task task)
        {
            MyDBCommand command = new MyDBCommand();
            command.Sql = "INSERT INTO test.Reviews VALUES(NULL, @UnitId, @Remark,@CompletedDateTime,@Sequence)";
            command.Parameters.Add("@UnitId", task.UnitId);
            command.Parameters.Add("@Remark", task.ToString());
            command.Parameters.Add("@CompletedDateTime", DateTime.Today);
            command.Parameters.Add("@Sequence", task.Sequnce);

            return db.InsertAutoCreatedId(command);
        }

        internal bool SetComplete(Task task)
        {
       
            List<MyDBCommand> Commands = new List<MyDBCommand>();

            MyDBCommand command1 = new MyDBCommand();
            command1.Sql = "Update test.reviews Set status=3,CompletedDatetime=@CompletedDatetime Where Id=@Id";
            command1.Parameters.Add("@Id",task.Id);
            command1.Parameters.Add("@CompletedDatetime", DateTime.Today);
            Commands.Add(command1);

            MyDBCommand command2 = GetNextTaskInsertCommand(task);
            Commands.Add(command2);

            if (task.Sequnce==1)
            {
                MyDBCommand command3 = GetNextUnitFirstNewTaskInsertCommand(task);
                if (command3 != null) Commands.Add(command3);
            }

            return db.ExcuteTranscation(Commands);
        }

        private MyDBCommand GetNextUnitFirstNewTaskInsertCommand(Task task)
        {
            Task newTask = GetNextUnitFirstNewTask(task);
            if (newTask==null)
            {
                return null;
            }
            MyDBCommand command = new MyDBCommand();
            command.Sql = "INSERT INTO test.Reviews VALUES(NULL, @UnitId, @Remark,Null,@Sequence,@PlanDateTime,@Status)";
            command.Parameters.Add("@UnitId", newTask.UnitId);
            command.Parameters.Add("@Remark", newTask.Description);
            //command.Parameters.Add("@CompletedDateTime", null);
            command.Parameters.Add("@Sequence", newTask.Sequnce);
            command.Parameters.Add("@PlanDateTime", newTask.ScheduledTime);
            command.Parameters.Add("@Status", newTask.Status);

            return command;
        }

        private Task GetNextUnitFirstNewTask(Task task)
        {
            string UnitId = GetNextUnitId(task.UnitId);
            if (string.IsNullOrEmpty(UnitId)) return null;//If There is not a next new unit ,return null;

            Unit unit = new UnitsManager().GetUnitById(UnitId);
            if (string.IsNullOrEmpty(UnitId))
            {
                return null;
            }

            int newSequence = 1;

            DateTime? newPlanDate = DateTime.Today.AddDays(1);
            string Decription = unit.ToString();
            Task newTask = new Task(newSequence, newPlanDate, Decription);
            newTask.UnitId = int.Parse(UnitId);
            newTask.Status = TaskStatus.Started;
            return newTask;

        }

        private string GetNextUnitId(int lastUnitId)
        {
            string NextUnitId = "";
            MyDBCommand command = new MyDBCommand();
            command.Sql = "select sequence,subjectId from test.units where Id=@Id";
            command.Parameters.Add("@Id",lastUnitId);
            DataTable dt=db.Query(command);
            DataRow dr=dt.Rows[0];
            int Sequence = DbHelper.GetIntValueFromRow("sequence", dr).Value;
            int SubjectId = DbHelper.GetIntValueFromRow("subjectId",dr).Value;

            MyDBCommand command2 = new MyDBCommand();
            command2.Sql = "select Id  from test.units where SubjectId=@SubjectId and sequence=@Sequence";
            command2.Parameters.Add("@SubjectId", SubjectId);
            command2.Parameters.Add("@Sequence", Sequence + 1);
            DataTable dt2=db.Query(command2);
            if (dt2==null || dt2.Rows.Count<=0)
            {
                return "";
            }
            DataRow dr2=dt2.Rows[0];
            NextUnitId = DbHelper.GetStringFromRow("Id", dr2);

            return NextUnitId;
        }


        private MyDBCommand GetNextTaskInsertCommand(Task task)
        {
            Task newTask = GetNextNewTask(task);

            MyDBCommand command = new MyDBCommand();
            command.Sql = "INSERT INTO test.Reviews VALUES(NULL, @UnitId, @Remark,Null,@Sequence,@PlanDateTime,@Status)";
            command.Parameters.Add("@UnitId", newTask.UnitId);
            command.Parameters.Add("@Remark", newTask.Description);
            //command.Parameters.Add("@CompletedDateTime", "");
            command.Parameters.Add("@Sequence", newTask.Sequnce);
            command.Parameters.Add("@PlanDateTime", newTask.ScheduledTime);
            command.Parameters.Add("@Status", newTask.Status);           

            return command;
        }

        private Task GetNextNewTask(Task task)
        {
            int newSequence = task.Sequnce + 1;
            IMemoryCurve Curve = CurveFactory.GetCurve();
            DateTime? newPlanDate = Curve.GetNextPlanDate(task.Sequnce, task.CompletedDateTime);
            string Decription = task.Description;
            Task newTask = new Task(newSequence,newPlanDate,Decription);
            newTask.UnitId = task.UnitId;
            newTask.Status = TaskStatus.Started;
            return newTask;
        }


    }
}
